package com.ngmaster.socket.connection;

import io.netty.buffer.ByteBuf;

/**
 * 网络链接
 * */
public abstract class NetConnection<T> {

	/**
	 *  链接唯一ID
	 */
	protected long connectionId = 0L;
	
	/**
	 * 会话
	 */
	protected T channelHandlerContext = null;

	
	/**
	 * 默认不等待
	 */
	protected boolean isWaitIng = false;
	
	/**
	 * 
	 */
	protected Object clientObject = null;
	
	/**
	 * 适配器Ip
	 */
	protected String connectionIp = "";
	
	/**
	 * 获取适配器的Ip
	 * @return
	 */
	public String getConnectionIp() {
		return connectionIp;
	}
	
	/**
	 * 设置网络适配器的Ip
	 */
	public void setConnectionIp(String connectionIp) {
		this.connectionIp = connectionIp;
	}
		
	/**
	 * 获取适配器的ID
	 * @return
	 */
	public long getConnectionId() {
		return connectionId;
	}
	
	/**
	 * 设置网络适配器的ID
	 * @param connectionId
	 */
	public void setConnectionId(long connectionId) {
		this.connectionId = connectionId;
	}
	
	/**
	 * 初始化
	 * @param connectionId
	 * @param channelHandlerContext
	 */
	public void init(long connectionId, T channelHandlerContext, Object coObject) {
		this.connectionId = connectionId;
		this.channelHandlerContext = channelHandlerContext;
		clientObject = coObject;
	}
	
	/**
	 * 获取对象
	 * @return
	 */
	public Object getClient() {
		return clientObject;
	}
	
	/**
	 * 获取会话对象
	 * @return
	 */
	public T getChannelHandlerContext() {
		return this.channelHandlerContext;
	}

	/**
	 * 发送消息
	 */
	public abstract void sendMessage(ByteBuf byteBuf);

	/**
	 * 获取客户端IP
	 * */
	public abstract String getClientIp();
	
	/**
	 * 设置等待
	 */
	public void setWaiting() {
		isWaitIng = true;
	}
	
	public boolean isWaiting() {
		return isWaitIng;
	}
	
	/**
	 * 关闭连接
	 */
	public abstract void close();
}
